<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
// check if the user is logged-in
include 'authentication.php';
isAuthorized();

include_once("../utils/redirect.php");
include_once($_SERVER["DOCUMENT_ROOT"]."/web-doc/protected/database/MySqlDAO.php");
// Contain variable "rootDir"
include_once('../utils/directories.php');

 $mySqlDAO = new MySqlDAO();

$parent_dir = $_GET['parent_dir'];
if(isset($_GET['idc']))
    $idc = $_GET['idc'];
if(isset($_GET['idf']))
    $idf = $_GET['idf'];
if(isset($_GET['idl']))
    $idl = $_GET['idl'];


if (empty($_POST['isToDelete'])) {
    if (!empty($idc)) 
        $name = $mySqlDAO->getCategoryName($idc);
    else if (!empty($idf)) 
        $name = $mySqlDAO->getFileTitle($idf);
    else if (!empty($idl)) 
        $name = $mySqlDAO->getLinkTitle($idl);    
}
else if (!empty($_POST['isToDelete'])) {
    
    // delete category
    if (!empty($idc)) {
        $dbPath = $mySqlDAO->getCategoryPath($idc);
        $res = $mySqlDAO->deleteCategory($idc);
        if (!empty ($res)) {
            $path = getRootDir() . $dbPath;
            rmdir($path) or die ("ERROR: Cannot delete directory " . $path);
            redirect("showcategory.php?idc=$parent_dir",FALSE);
        } else
            die ("ERROR: Cannot delete category " . $name);
    } 
    // delete file
    else if (!empty($idf)) {
        $dbPath = $mySqlDAO->getFilePath($idf);
        $fileName = $mySqlDAO->getFileName($idf);
        $res = $mySqlDAO->deleteFile($idf);
        if (!empty ($res)) {
            // Contain variable "rootDir"
            include_once('../utils/directories.php');
            $path = getRootDir() . $dbPath . $fileName;
            unlink($path) or die ("ERROR: Cannot delete file " . $path);
            redirect("showcategory.php?idc=$parent_dir",FALSE);
        } else
            die ("ERROR: Cannot delete file " . $fileName);
        
    } 
    // delete link
    else if (!empty($idl)) {
        $res = $mySqlDAO->deleteLink($idl);
        if (!empty ($res)) 
            redirect("showcategory.php?idc=$parent_dir",FALSE);
        else
            die ("ERROR: Cannot delete link");
    } else {
        die ("ERROR: Inserire tutti i dati. <a href=javascript:history.back(-1)>Indietro</a>");
    }
}
?>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../styles/main.css">
        <link rel="stylesheet" type="text/css" href="../styles/admin.css">
        <title>WebDoc - Delete element</title>
    </head>
    <body>
        <?php
            if(isset($idc))
                $formaction = "deleteelement.php?parent_dir=$parent_dir&idc=$idc";
            if(isset($idf))
                $formaction = "deleteelement.php?parent_dir=$parent_dir&idf=$idf";
            if(isset($idl))
                $formaction = "deleteelement.php?parent_dir=$parent_dir&idl=$idl";
        ?>
        <form method="post" enctype="multipart/form-data" action="<?php echo $formaction ?>">
            <table width="450" border="0" cellpadding="1" cellspacing="1" class="box">
                <tr>
                    <td valign="top" colspan="2">
                        <?php
                        if (!empty($idc))
                            echo "Eliminare la categoria '$name'?";
                        else if (!empty($idf))
                            echo "Eliminare il file '$name'?";
                        else if (!empty($idl))
                            echo "Eliminare il link '$name'?";
                        ?>

                        <input type="hidden" name="isToDelete" value="true">
                    </td>
                </tr>
                <tr>
                    <td width="80" colspan="2">
                        <br><br>
                        <input type="submit" value="Elimina" />
                        <!--<input name="upload" type="submit" class="box" id="upload" value=" Upload ">-->
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
